import axios from 'axios';

const HOST = process.env.VUE_APP_PROJECT_REQUEST_HOST;

function getDepartmentId(departmentName) {
    return new Promise((resolve, reject) => {
        axios
            .get(`${HOST}/api/base/department/exist?name=${departmentName}`)
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                resolve({ id: null });
            });
    });
}

function getSubProjectId(subProjectName) {
    return new Promise((resolve, reject) => {
        axios
            .get(
                `${HOST}/api/base/project/exist?project_name=${subProjectName}`
            )
            .then((response) => {
                resolve(response.data);
            })
            .catch((err) => {
                console.log(err);
                resolve({ id: null });
            });
    });
}

// 通过部门ID查询部门信息，包含事业群
function getDepartmentById(id) {
    return new Promise((resolve, reject) => {
        try {
            const url = `${HOST}/api/base/department/${id}`;
            axios.get(url).then((response) => {
                if (response.data.code === 200) {
                    let respData = response.data.data;
                    let deptData = {
                        business_department: respData.name,
                        business_department_id: respData.id,
                        business_department_manager: respData.manager,
                        business_group: respData.business_group.name,
                        business_group_id: response.data.data.business_group.id,
                        business_group_manager: respData.business_group.manager,
                    };
                    // let incomeDistribution = this.form.incomeDistribution || [];
                    // if (incomeDistribution.length) {
                    // 	incomeDistribution[0].manager_id =
                    // 		this.form.project_manager_id
                    // 	incomeDistribution[0].manager =
                    // 		this.form.project_manager
                    // 	incomeDistribution[0].implement_department_id =
                    // 		response.data.data.id;
                    // 	incomeDistribution[0].implement_department =
                    // 		response.data.data.name;
                    // }
                    resolve(deptData);
                }
            });
        } catch (error) {
            reject(console.log(error));
        }
    });
}

function createFilter(queryString) {
    return (item) => {
        return (
            item.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        );
    };
}

function querySearchProjectNameAsync(queryString, cb) {
    const url = `${HOST}/api/base/project/query`;
    if (queryString != undefined && queryString.length > 2) {
        axios
            .get(url, { params: { name: queryString } })
            .then((response) => {
                let projects = response.data.data.map((project) => {
                    return {
                        value: project.name,
                        id: project.id,
                    };
                });
                var results = queryString
                    ? projects.filter(createFilter(queryString))
                    : projects;
                cb(results);
            })
            .catch((error) => {
                console.log(error);
            });
    } else {
        cb(new Array());
    }
}

export {
    getDepartmentId,
    getSubProjectId,
    getDepartmentById,
    querySearchProjectNameAsync,
};
